﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;

namespace METU_CMT.Business.Google
{
    public class GoogleCalendarAdapter
    {
        private string email;
        private string password;

        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public void ExportToGoogleCalendar(List<Event> schedule)
        {
            Service service = new Service("cl", "METU CMT calendar");
            service.setUserCredentials(email, password);

            foreach (var _event in schedule)
            {
                EventEntry entry = new EventEntry();
                AtomPerson author = new AtomPerson(AtomPersonType.Author);
                author.Name = "METU CMT";
                author.Email = email;
                entry.Authors.Add(author);
                entry.Title.Text = _event.Description;
                entry.Content.Content = _event.Description + " Submission exists: " + _event.SubmissionExist;

                When eventTimes = new When();
                eventTimes.StartTime = _event.DueDate;
                eventTimes.EndTime = _event.DueDate;
                entry.Times.Add(eventTimes);

                Uri postUri = new Uri("http://www.google.com/calendar/feeds/" + email + "/private/full");
                service.Insert(postUri, entry);
            }

            
        }
    }
}